package com.vhall.component.service.guosen.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vhall.component.dao.guosen.GuosenIfcPeHoldMapper;
import com.vhall.component.entity.guosen.entity.GuosenIfcPeHoldEntity;
import com.vhall.component.plugin.common.constant.RedisKey;
import com.vhall.component.plugin.common.utils.RedisLocker;
import com.vhall.component.service.guosen.GuosenIfcPeHoldService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
* @author a1111
* @description 针对表【guosen_ifc_pe_hold(私募基金持仓表)】的数据库操作Service实现
* @createDate 2023-10-30 11:04:38
*/
@Service
public class GuosenIfcPeHoldServiceImpl extends ServiceImpl<GuosenIfcPeHoldMapper, GuosenIfcPeHoldEntity>
    implements GuosenIfcPeHoldService {
    @Resource
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public Set<String> getCacheAllCustIDAndFundIDList() {
        Set<String> result = new HashSet<>();
        if (redisTemplate.hasKey(RedisKey.ALL_GUO_SEN_FUNDID_SET)) {
            result = redisTemplate.opsForSet().members(RedisKey.ALL_GUO_SEN_FUNDID_SET);
        }else{
            if (RedisLocker.tryLock(RedisKey.ALL_GUO_SEN_FUNDID_SET,"1",1000)) {
                LambdaQueryWrapper<GuosenIfcPeHoldEntity> queryWrapper = Wrappers.lambdaQuery();
                List<GuosenIfcPeHoldEntity> list = list(queryWrapper);
                result = list.stream().map(GuosenIfcPeHoldEntity::getFundid).collect(Collectors.toSet());
                if (CollUtil.isEmpty(result)) {
                    redisTemplate.opsForSet().add(RedisKey.ALL_GUO_SEN_FUNDID_SET,"");
                }else{
                    redisTemplate.opsForSet().add(RedisKey.ALL_GUO_SEN_FUNDID_SET,result.toArray(String[]::new));
                }

                redisTemplate.expire(RedisKey.ALL_GUO_SEN_FUNDID_SET,1, TimeUnit.MINUTES);
            }
            RedisLocker.unlock(RedisKey.ALL_GUO_SEN_FUNDID_SET,"1");
        }
        return result;
    }
}




